import { load_external_img } from "../framework/Utils";
import { ServerConfig } from "../config/ServerConfig";

export default class Tool {
    static starColor = [
        cc.color(206,223,233,255),
        cc.color(191,255,143,255),
        cc.color(143,215,255,255),
        cc.color(186,144,254,255),
        cc.color(255,199,144,255)
    ];
    static starTxt = ["普通", "优秀", "名宿", "巨星", "传奇"];

    static getRandomIntNum(min:number,max:number) {
        let Range = max - min;
        let Rand = Math.random();
        return (min + Math.round(Rand * Range));
    }

    static getRandomFloatNum(min:number,max:number) {
        let Range = max - min;
        let Rand = Math.random();
        return (min + Rand * Range);
    }

    static stringFormat (formatSrt: string, ...args: any[]) {
        if (arguments.length == 0) {
            return null;
        }
        var str = arguments[0];
        for (var i = 1; i < arguments.length; i++) {
            var re = new RegExp('\\{' + (i - 1) + '\\}', 'gm');
            str = str.replace(re, arguments[i]);
        }
        return str;
    }

    static formatTime(t) {
        let m = Math.floor(t / 60)
        let s = t - m * 60

        let ms = ""
        if (m < 10) {
            ms = "0" + m
        }else {
            ms = m.toString()
        }
        let ss = ""
        if (s < 10) {
            ss = "0" + s
        }else {
            ss = s.toString()
        }
        return ms +":" + ss
    }

    static timestampToTime(timestamp) {
        let H = Math.floor(timestamp / 3600)
        let M = Math.floor(timestamp / 60) - H * 60
        let S = timestamp - H * 60 * 60 - M * 60
        let h = H < 10 ? '0'+H+ ':' : H+ ':';
        let m = M < 10 ? '0'+M+ ':' : M+ ':';
        let s = S< 10 ? '0'+S : S;
        return h+m+s;
    }

    static parserFormation (val:string) {
        const vals:string[] = val.split('-');
        const va1:number = parseInt(vals[0]);
        const va2:number = parseInt(vals[1]) + va1;
        const va3:number = parseInt(vals[2]) + va2;

        let ret: number[] = [];
        for (let i = 0; i < 8; i++) {
            if (i < va1) {
                ret.push(3)
            } else if (i < va2)  {
                ret.push(2)
            } else if (i < va3)  {
                ret.push(1)
            }
        }
        return ret
    }

    static load_card_head (sprite:cc.Sprite, img_url:string) {
        if (ServerConfig.STATUS == 1) {
            load_external_img(sprite, ServerConfig.STATIC_URL + "card/" + img_url + ".png","png");
        }
    }

    static load_card_pos (sprite:cc.Sprite, pos) {
        load_external_img(sprite, ServerConfig.STATIC_URL + "common/card_pos" + pos + ".png","png");
    }

    static load_card_bg (sprite:cc.Sprite, star) {
        load_external_img(sprite, ServerConfig.STATIC_URL + "common/card_bg" + star + ".png","png");
    }

    static load_card_fg (sprite:cc.Sprite, formatonPos) {
        load_external_img(sprite, ServerConfig.STATIC_URL + "common/card_fg" + formatonPos + ".png","png");
    }

    static load_player_star_color (node:cc.Node, star) {
        node.color = this.starColor[star - 1];
    }

    static load_player_star_txt (comp:cc.Label, star) {
        comp.string = this.starTxt[star - 1];
    }

    static load_gift_img (sprite:cc.Sprite, star) {
        load_external_img(sprite, ServerConfig.STATIC_URL + "common/ico_card0" + star + ".png","png");
    }

    static load_gym_img (sprite:cc.Sprite, level) {
        load_external_img(sprite, ServerConfig.STATIC_URL + "common/fra_gym0" + level + ".png","png");
    }

    static load_user_avatar (sprite:cc.Sprite, img_url) {
        load_external_img(sprite, img_url,"png");
    }
}